﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameOverShow : MonoBehaviour {
    public UISprite hucard;
    public UISprite sister;
    public List<EveryJuHuInfoItem> everyJuHuInfoItem;
    public GameObject EveryJu;
    //public GameObject flow;
    public GameObject Collider;
    public GameObject GameShow;
    public List<GameObject> Down;
    public List<GameObject> Right;
    public List<GameObject> Up;
    public List<GameObject> Left;

    public GameObject GameOverInfo;

    EginMahjong mahjong = EginMahjong.Instance;
    public GameObject ExitRoomBtn;
    public GameObject NextBtn;


    public bool botDismiss = false;

    public GameObject EveryRound;
    private void Awake()
    {
        UIEventListener.Get(ExitRoomBtn).onClick += BtnClick;
        UIEventListener.Get(NextBtn).onClick += BtnClick;
    }
    private void Start()
    {
        //loadTest();       
    }
    void loadTest() {
        Down[0].SetActive(true);

    }
    void BtnClick(GameObject ButtonClick) {
        if (ButtonClick.name == "ExitRoomBtn") {

            if (botDismiss)
            {
                mahjong.OverRoundScore.Clear();
                mahjong.TotalScore.Clear();
                mahjong.OverRank.Clear();
                mahjong.OverHandCardList.Clear();
                mahjong.OtherCoinCount.Clear();
                mahjong.OtherScore.Clear();
                mahjong.currentRound = 0;
                mahjong.OverRecordList.Clear();
                mahjong.OverRobot.Clear();
                mahjong.Otheruid.Clear();
                mahjong.OtherSeatId.Clear();
                mahjong.OtherSeatStatus.Clear();
                mahjong.OtherSex.Clear();
                mahjong.OtherIP.Clear();
                mahjong.OtherNickName.Clear();
                mahjong.OtherAvatarid.Clear();
                mahjong.bankerSid = 0;
                mahjong.mahjongSpecialCardItem.Clear();
                Application.LoadLevel("Hall");
                
            }
            else {
                RoomLeaveRequest RR = new Pb.RoomLeaveRequest { RoomId = mahjong.roomID };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(RR, MsgId.RoomLeaveRequest);
            }
            
           
        }
        else if (NextBtn.name == "NextBtn") {
            RoomReadyRequest RR = new Pb.RoomReadyRequest { RoomId = mahjong.roomID };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.RoomReadyRequest);
            Destroy(gameObject);
        }
    }



    public void LoadGameOver(bool IsFlow ,bool IsAllOver,List<GameOverUserInfo> gameOverUserInfo) {
        if (IsAllOver)
        {
            EveryRound.SetActive(false);
            GameObject MahjongSettlement = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load("GameMahjong/MahjongSettlement"))); //用牛牛的结算界面
            MahjongSettlement.GetComponent<NNSettlement>().LoadInfo(gameOverUserInfo);
        }
        else {
            if (IsFlow)
            {
                hucard.spriteName = "liuju";
            }
            else
            {
                if (mahjong.OverRoundScore[mahjong.seatID] >= 0)
                {
                    hucard.spriteName= "shengli";
                }
                else {
                    hucard.spriteName = "shibai";
                }
            }

            if (mahjong.OverRoundScore[mahjong.seatID] >= 0)
            {
                sister.spriteName = "ying";
            }
            else
            {
                sister.spriteName = "shu";
            }
            Collider.SetActive(false);
            Collider.SetActive(true);
            GameShow.SetActive(false);
            LoadIndex();
            foreach (KeyValuePair<int, List<int>> kvp in mahjong.OverHandCardList)
            {
                #region 判断加载位置
                if (returnOtherSeatIndex(kvp.Key) == 1)
                {
                    BubbleSort(kvp.Value);
                    for (int i = 0; i < kvp.Value.Count; i++)
                    {
                        Down[i].SetActive(true);
                        Down[i].GetComponent<UISprite>().spriteName = kvp.Value[i].ToString();
                        //Down[i].name = kvp.Value.ToString();
                    }
                }
                else if (returnOtherSeatIndex(kvp.Key) == 2)
                {
                    kvp.Value.Sort();
                    for (int i = 0; i < kvp.Value.Count; i++)
                    {
                        Right[i].SetActive(true);
                        Right[i].GetComponent<UISprite>().spriteName = kvp.Value[i].ToString();
                        //Right[i].name = kvp.Value.ToString();
                    }
                }
                else if (returnOtherSeatIndex(kvp.Key) == 3)
                {
                    kvp.Value.Sort();
                    for (int i = 0; i < kvp.Value.Count; i++)
                    {
                        Up[i].SetActive(true);
                        Up[i].GetComponent<UISprite>().spriteName = kvp.Value[i].ToString();
                        //Up[i].name = kvp.Value.ToString();
                    }
                }
                else if (returnOtherSeatIndex(kvp.Key) == 4)
                {
                    kvp.Value.Sort();
                    for (int i = 0; i < kvp.Value.Count; i++)
                    {
                        Left[i].SetActive(true);
                        Left[i].GetComponent<UISprite>().spriteName = kvp.Value[i].ToString();
                        //Left[i].name = kvp.Value.ToString();
                    }
                }
                #endregion 判断加载位置
            }
            //GameOverInfo.SetActive(true);
            //LoadScore();

            //加载胡牌
            if (!IsFlow)
            {
                int index = returnOtherSeatIndex(mahjong.Huseatid);
                int huIndex = mahjong.OverHandCardList[mahjong.Huseatid].Count + 1;
                if (index == 1)
                {
                    Down[14].SetActive(true);
                    Down[14].GetComponent<UISprite>().spriteName = mahjong.HuCard.ToString();
                }
                else if (index == 2)
                {
                    Right[huIndex].SetActive(true);
                    Right[huIndex].GetComponent<UISprite>().spriteName = mahjong.HuCard.ToString();
                }
                else if (index == 3)
                {
                    Up[huIndex].SetActive(true);
                    Up[huIndex].GetComponent<UISprite>().spriteName = mahjong.HuCard.ToString();
                }
                else if (index == 4)
                {
                    Left[14].SetActive(true);
                    Left[14].GetComponent<UISprite>().spriteName = mahjong.HuCard.ToString();
                }
                if (mahjong.whatHu == MAHJONG_ACTION_TYPE.GangHu || mahjong.whatHu == MAHJONG_ACTION_TYPE.SelfHu)
                {
                    //ZiMo[index - 1].SetActive(true);
                }
                else if (mahjong.whatHu == MAHJONG_ACTION_TYPE.NormalHu)
                {
                    // Hu[index - 1].SetActive(true);
                }
            }

            for (int i = 0; i < 4; i++)
            {
                everyJuHuInfoItem[i].isflow = IsFlow;
                everyJuHuInfoItem[i].LoadInfo(i + 1, gameOverUserInfo);                
                everyJuHuInfoItem[i].gameObject.SetActive(true);
            }
            Debug.Log("结算信息显示完毕");
        }
        
    }



   

    #region 辅助计算
   
    /*-玩家显示-*/
    int DownIndex;//本人玩家在字典索引
    int RightIndex;
    int TopIndex;
    int LeftIndex;
    List<int> tempList = new List<int>();
    void LoadIndex() {
        foreach (KeyValuePair<int, int> kvp in mahjong.Otheruid)
        {
            if (mahjong.seatID == kvp.Key)
            {
                DownIndex = kvp.Key;
                Debug.Log("本人玩家所在索引" + DownIndex);
                break;
            }
        }
        #region 判断其他玩家生成位置逆时针出牌
        //判断其他玩家生成位置
        if (DownIndex == 1 || DownIndex == 0)  //不加DownIndex==0需要在创建房间成功时候把自己的信息存入EginUser里面的OtherUser信息里面
        {
            RightIndex = 2;
            TopIndex = 3;
            LeftIndex = 4;
        }
        else if (DownIndex == 2)
        {
            RightIndex = 3;
            TopIndex = 4;
            LeftIndex = 1;
        }
        else if (DownIndex == 3)
        {
            RightIndex = 4;
            TopIndex = 1;
            LeftIndex = 2;
        }
        else if (DownIndex == 4)
        {
            RightIndex = 1;
            TopIndex = 2;
            LeftIndex = 3;
        }
        tempList.Add(DownIndex);
        tempList.Add(RightIndex);
        tempList.Add(TopIndex);
        tempList.Add(LeftIndex);
        #endregion 判断其他玩家生成位置逆时针出牌
    }

    private int returnOtherSeatIndex(int seatid)
    {

        int tempReture = mahjong.returnOtherSeatIndex(seatid);

        return tempReture;
    }
    #endregion

    #region 冒泡排序从大到小
    private void BubbleSort(List<int> R)
    {
        int len = R.Count;
        Debug.Log(R.Count);
        bool flag = false;
        for (int i = 0; i < len - 1; i++)
        {
            flag = false;
            for (int j = 0; j < len - i - 1; j++)
            {
                if (R[j] < R[j + 1])
                {
                    Swap(R[j], R[j + 1], j, R);
                    flag = true;
                }
            }
            if (!flag)
            {
                break;
            }
        }

    }
    private void Swap(int left, int right, int j, List<int> R)
    {
        int temp = left;
        R[j] = right;
        R[j + 1] = temp;
    }
    #endregion

}
